package com.codeest.geeknews.util;

import android.webkit.JavascriptInterface;

import com.tencent.smtt.sdk.WebView;

/**
 * 在asset下存放webview.html
 * <html>
 * <head>
 * <meta charset="utf-8"/>
 * <meta name="viewport"
 * content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
 * </head>
 * <body>
 * <div id="content" class="webview-body"></div>
 * <script type="text/javascript">
 * function setUpContent(content) {
 * console.log("content" + content );
 * document.getElementById('content').innerHTML = content;
 * alignScroll();
 * bindImageClickEvent();
 * }
 * function alignScroll() {
 * window.handler.onHTMLGenerated();
 * }
 * function bindImageClickEvent(){
 * var objs = document.getElementsByTagName("img");
 * var images = new Array();
 * for(var i=0;i<objs.length;i++)
 * {
 * clazz_ = objs[i].getAttribute("class");
 * var isskip  =( clazz_ == null? false: clazz_ .indexOf("skip") != -1 ) ;
 * if(!isskip){
 * console.log("src-->" + objs[i].src );
 * images[images.length ++] =objs[i].src ;
 * objs[i].onclick = function(url,position)
 * {
 * return function()
 * {
 * window.handler.openImage(url,position,images);
 * return false;
 * };
 * }(objs[i].src,images.length-1 );
 * }
 * }
 * }
 * </script>
 * </body>
 * </html>
 * Created by admin on 2017/7/13.
 */
public class WebViewJsUtil {
    private WebView mWebView;

    public WebViewJsUtil(WebView webView, Object target) {
        if (!(target instanceof OnJsInteractionListener)) {
            throw new RuntimeException(target.toString()
                    + " must implement OnJsInteractionListener");
        }
        this.mWebView = webView;
        bindJavascriptInterface(target);
    }

    /**
     * 重写方法要加@JavascriptInterface
     */
    public interface OnJsInteractionListener {
        /**
         * html完整页面生成回调
         */
        @JavascriptInterface
        void onHTMLGenerated();

        /**
         * 点击img标签回调
         *
         * @param url-点击图片的地址
         * @param position-点击图片的索引
         * @param allUlrs-页面内所有图片地址数组
         */
        @JavascriptInterface
        void openImage(String url, int position, String[] allUlrs);
    }

    private void bindJavascriptInterface(Object target) {
        mWebView.addJavascriptInterface(target, "handler");
    }

    public void init() {
        loadAssertFile();
    }

    private void loadAssertFile() {
        mWebView.loadUrl("file:///android_asset/webview.html");
    }

    public void loadData(String str) {
        str = str.replaceAll("(\\r|\\n|\\r\\n)+", "\\\\n");
        mWebView.loadUrl("javascript:setUpContent( \'" + str + "\' )");
    }

    //
    public void loadUrl(String url) {
        mWebView.loadUrl(url);
    }

    public void loadJs() {
        String js = "javascript: function bindImageClickEvent(){" +
                "            var objs = document.getElementsByTagName(\"img\");" +
                "            var images = new Array();" +
                "            for(var i=0;i<objs.length;i++)" +
                "            {" +
                "                clazz_ = objs[i].getAttribute(\"class\");" +
                "                var isskip  =( clazz_ == null? false: clazz_ .indexOf(\"skip\") != -1 ) ;" +
                "                if(!isskip){" +
                "                    console.log(\"src&ndash;&gt;\" + objs[i].src );" +
                "                    images[images.length ++] =objs[i].src ;" +
                "                    objs[i].onclick = function(url,position)" +
                "                                    {" +
                "                                        return function()" +
                "                                               {" +
                "                                                     window.handler.openImage(url,position,images);" +
                "                                                     return false;" +
                "                                               };" +
                "                                    }(objs[i].src,images.length-1 );" +
                "                }" +
                "            }" +
                "        }";
        mWebView.loadUrl(js);
        //mWebView.loadUrl("javascript:myFunction()");
    }
}
